package main

/*
Definition for a binary tree node.
*/

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func rob(root *TreeNode) int {
	res := postOrder337(root)
	return max337(res[0], res[1])
}

func postOrder337(root *TreeNode) []int {
	if root == nil {
		return []int{0, 0}
	}

	l := postOrder337(root.Left)
	r := postOrder337(root.Right)

	robCur := root.Val + l[0] + r[0]
	notRobCur := max(l[0], l[1]) + max(r[0], r[1])
	return []int{notRobCur, robCur}
}

func max337(a, b int) int {
	if a > b {
		return a
	}
	return b
}
